var page = require('webpage').create(),
    system = require('system'),
    fs = require('fs'),
    url = 'http://t.10jqka.com.cn/guba/002222/',
    t = Date.now();

function writeChat(model) {
  var file = fs.open("text.json", 'r');
  file.close();
  file = fs.open("text.json", 'w');
  file.write(JSON.stringify(model));
  file.close();
}

function consoleText() {
  page.open(url, function(status) {

    if (status !== 'success') {
      console.log('抓取错误');
    } else {
      console.log('抓取成功');
      var item = page.evaluate(function() {
        var list = [];
        var data = document.querySelectorAll('.chat-list > .chat-data-single');

        for (var i = 0;i < data.length;i++) {
          list.push(data[i].querySelector('.chat-txt').innerText)
        };

        return list;
      })

      writeChat(item);
      setTimeout(function() {
        consoleText();
      }, 6000);
    }
  });
}

consoleText();
